/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * WardPanel.java
 *
 * Created on 06.07.2009, 22:37:40
 */

package ru.vsu.cs.piit.vokod.view.panels;

import java.awt.Color;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import javax.swing.InputVerifier;
import javax.swing.JComponent;
import javax.swing.JTextField;
import javax.swing.border.LineBorder;
import ru.vsu.cs.piit.vokod.model.Ward;

/**
 *
 * @author Галчонок
 */
public class WardPanel extends javax.swing.JPanel {
    Ward ward = new Ward();

    /** Creates new form WardPanel */
    public WardPanel() {
        initComponents();

        InputVerifier verifier = new InputVerifier() {
            @Override
          public boolean verify(JComponent comp) {
            boolean returnValue;
            JTextField textField = (JTextField) comp;
            if (textField.getText().compareTo("")!=0)
                try {
                  Integer.parseInt(textField.getText());
                  textField.setBorder(new LineBorder(Color.BLACK, 1));
                  returnValue = true;
                } catch (NumberFormatException e) {
                    textField.setBorder(new LineBorder(Color.RED, 1));
                    returnValue = false;
                }
            else returnValue = true;
            return returnValue;
          }
        };

        jtfBeds.setInputVerifier(verifier);
        this.setSize(this.getPreferredSize());
        AddActions();
    }

    public WardPanel(Ward ward) {
        this();
        this.ward = ward;
        setValues();
    }

    private void setValues() {
        this.jtfName.setText(ward.getName());
        this.jtfCode.setText(ward.getCode());
    }

    private void AddActions(){
        jtfCode.addFocusListener(new FocusListener() {

            @Override
            public void focusGained(FocusEvent e) {}

            @Override
            public void focusLost(FocusEvent e) {
                ward.setCode(jtfCode.getText());
            }
        });

        jtfName.addFocusListener(new FocusListener() {

            @Override
            public void focusGained(FocusEvent e) {}

            @Override
            public void focusLost(FocusEvent e) {
                ward.setName(jtfName.getText());
            }
        });

        jtfBeds.addFocusListener(new FocusListener() {

            @Override
            public void focusGained(FocusEvent e) {}

            @Override
            public void focusLost(FocusEvent e) {
                if (jtfBeds.getText()!=null)
                    ward.setBedsCount(Integer.parseInt(jtfBeds.getText()));
            }
        });
    }

    public Ward getWard() {
        return ward;
    }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jtfName = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jtfCode = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jtfBeds = new javax.swing.JTextField();

        jLabel1.setText("Старое название (ДИАГ, ООГ, и т.д.)");

        jLabel2.setText("Новое название (ОО7, ОО5, и т.д.)");

        jLabel3.setText("Развернуто коек");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jtfCode, javax.swing.GroupLayout.DEFAULT_SIZE, 236, Short.MAX_VALUE)
                    .addComponent(jtfName, javax.swing.GroupLayout.DEFAULT_SIZE, 236, Short.MAX_VALUE)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2)
                    .addComponent(jtfBeds, javax.swing.GroupLayout.DEFAULT_SIZE, 236, Short.MAX_VALUE)
                    .addComponent(jLabel3))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jtfName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jtfCode, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jtfBeds, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(17, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JTextField jtfBeds;
    private javax.swing.JTextField jtfCode;
    private javax.swing.JTextField jtfName;
    // End of variables declaration//GEN-END:variables


}
